package com.tobase64;

import com.lcx.annotation.IOCValue;
import com.lcx.annotation.Init;
import com.lcx.annotation.JsObject;
import com.me.common.ToolsList;
import com.me.util.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Base64;

@IOCValue
@JsObject(name = "base64Util")
public class Base64Convert {

    @JsObject
    public String toBase64(String path) throws Exception{
        File file = new File(path);
        if(!file.exists()){
            throw new FileNotFoundException("file not found in "+path);
        }
        return Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(file));
    }

    @Init
    public void addList(){
        ToolsList.addTools(new ToolsList.Tools("_toBase64","文件转base64","file_drop/dropper.html","","版本：v1.0<br>powerd by Shisan.<br>说明：可将文件转为base64编码，注意，只能转5M以内的文件。"),Base64Convert.class);
    }

}
